import { UI_Listen, UI_Layer } from '../../config/ui_def'
import { SHOW_ITEM_TYPE } from '../../config/mod_def';
import item from '../base/item'

cc.Class({
    extends: item,

    properties: {
        light: cc.Node,
        num_label: cc.Label,
        icon_sprite: cc.Sprite,
        enough_sprite: cc.Sprite,
        _num: 0,                // 数量
        num: {
            get: function () {
                return this._num
            },
            set: function (num) {
                this.setNum(num)
            }
        },
    },
    initInfo(param, type, index) {
        if (parseInt(param) == 51001) {
            this.param = 51001
            this.head_sp.node.active = true
            this.icon_sprite.spriteFrame = he.load.get('fightMain', cc.SpriteAtlas, 'plist').getSpriteFrame(`head_1003`)
            return
        }

        this.node.getChildByName('background').active = true
        this.isCanShowDetail = false
        let info
        if (index >= 0) { this.index = index }
        this.enough_sprite.node.active = false
        if (param) {
            switch (type) {
                case SHOW_ITEM_TYPE.BY_CHECK:
                    let bc_param = param.split("_")
                    info = he.db.getData("Item", parseInt(bc_param[0]))
                    this.showCheckBorder(param)
                    break
                case SHOW_ITEM_TYPE.BY_INFO:
                    info = param
                    break
                case SHOW_ITEM_TYPE.BY_ID:
                default:
                    info = he.db.getData("Item", param)
                    break;
            }
            this.info = info    // 信息覆盖
            this.setIcon()
            this.setNum(0)
        }
    },

    canShowDetail() {
        this.isCanShowDetail = true
    },

    setIcon() {
        this.head_sp.node.active = true
        // this.icon_sprite.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame("" + this.info.icon)
        if (this.info.type != 3) {
            this.icon_sprite.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.id}`)
        } else if (this.info.type == 3) {
            this.icon_sprite.spriteFrame = he.load.get('skill', cc.SpriteAtlas, 'plist').getSpriteFrame(`${this.info.icon}`)
        }
    },
    showCheckBorder(param) {
        this.enough_sprite.node.active = true
        if (gg.data.checkItem(param)) {
            this.enough_sprite.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame("green")
        } else {
            this.enough_sprite.spriteFrame = he.load.get('item', cc.SpriteAtlas, 'plist').getSpriteFrame("red")
        }
    },
    setNum(num) {
        if (this.param == 51001) {
            this.num_label.node.active = true
            this.num_label.string = num
            return
        }

        if (!num) {
            num = 0
        }
        this._num = num
        if (this.info.overlay && num > 0) {
            this.num_label.node.active = true
        } else {
            this.num_label.node.active = false
        }
        this.num_label.string = num
    },

    setLabString(num) {
        this.num_label.node.active = true
        this.num_label.string = num
    },

    clickServer() {
        if (this.isChoose) {
            this.isChoose = false
            // return  
        }
        if (this.isCanShowDetail) {
            gg.uiManager.addPage(UI_Layer.Cover, {
                name: 'itemDetailPage',
                info: this.info,
                cover: true,
                close: true
            })
        } else {
            gg.eventManager.emit('bagItem', { type: UI_Listen.BAG_CHOOSE, index: this.index, info: this.info })
        }
    },

    showLight() {
        this.light.active = true
        this.isChoose = true
    },
    hideLight() {
        this.light.active = false
        this.isChoose = false
    },

    showItem() {
        this.head_sp.node.active = true
    },

    hideItem() {
        this.num_label.node.active = false
        this.head_sp.node.active = false
    },

});
